let threeSum = function (nums) {
    let res = [];
    const length = nums.length;
    if (nums == null || length <= 2) return res;
    nums.sort((a, b) => a - b); // 先排序
    for (let index = 0; index < length; index++) {
        if (nums[index] > 0) break;
        if (index > 0 && nums[index] == nums[index - 1]) continue;
        let left = index + 1;
        let right = length - 1;
        while (left < right) {
            let sum = nums[index] + nums[left] + nums[right];
            if (sum == 0) {
                res.push([nums[index], nums[left], nums[right]]);
                // 去重
                while (left < right && nums[left] == nums[left + 1]) left++;
                while (left < right && nums[right] == nums[right - 1]) right--;
                left++;
                right--;
            } else if (sum < 0) {
                left++;
            } else {
                right--;
            }
        }

    }
    return res;
}


